package com.eduspace.eduplatform.wares.utils;

import lombok.extern.slf4j.Slf4j;
import pub.tbc.toolkit.Objs;
import pub.tbc.toolkit.config.Config;
import pub.tbc.toolkit.config.ConfigProvider;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @author tbc
 * @version 1.0 {2016年7月29日 下午6:45:34}
 */
@Slf4j
public final class RedisUtil {

    private static Config redisConfig = ConfigProvider.getPropertiesConfig();

    private RedisUtil() throws Exception {
        throw new Exception("no " + getClass() + " instance for you");
    }

    private static JedisPoolConfig poolConfig = new JedisPoolConfig();
    private static JedisPool pool;

    static {
        String host = redisConfig.get("cache.redis.host");
        int port = redisConfig.getInt("cache.redis.port");

        pool = new JedisPool(poolConfig, host, port, 0);
    }

    public static Jedis getClient() {
        return pool.getResource();
    }

    public static void returnPool(Jedis client) {
        if (Objs.nonEmpty(client)) {
            //新版本jedis可以jedis.close()将连接归还连接池，旧版本不能这样还->pool.return...
            client.close();
        }
    }


}
